package com.tianma315.admin.produce.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.common.base.CoreServiceImpl;
import com.tianma315.admin.device.domain.MaintainRecordDO;
import com.tianma315.admin.marshalling.domain.MarshallingDO;
import com.tianma315.admin.marshalling.service.MarshallingService;
import com.tianma315.admin.produce.VO.ProduceVO;
import com.tianma315.admin.product.dao.ProductDao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianma315.admin.produce.dao.ProduceTaskDao;
import com.tianma315.admin.produce.domain.ProduceTaskDO;
import com.tianma315.admin.produce.service.ProduceTaskService;

import java.util.Calendar;
import java.util.List;

/**
 * 
 * <pre>
 * 生产任务
 * </pre>
 * <small> 2019-05-29 16:16:04 | Aron</small>
 */
@Service
public class ProduceTaskServiceImpl extends CoreServiceImpl<ProduceTaskDao, ProduceTaskDO> implements ProduceTaskService {

    @Autowired
    private ProduceTaskDao produceTaskDao;

    @Autowired
    private MarshallingService marshallingService;

    @Override
    public Page<ProduceVO> getProduceVOPage(Integer pageNumber, Integer pageSize, ProduceTaskDO produceTaskDTO) {
        Page<ProduceVO> produceVOPage = new Page(pageNumber,pageSize);
        List<ProduceVO> produceVOList = produceTaskDao.getProduceVOPage(produceVOPage,produceTaskDTO);
        produceVOPage.setRecords(produceVOList);
        return produceVOPage;
    }

    @Override
    public Boolean addProduceTaskDO(ProduceTaskDO produceTaskDO) {
        produceTaskDO.setCreateDate(Calendar.getInstance().getTime());
        produceTaskDO.setState(0);
        return insert(produceTaskDO);
    }

    @Override
    public Boolean deleteProduceTaskDO(Integer id) {
        ProduceTaskDO produceTaskDO = new ProduceTaskDO();
        produceTaskDO.setState(1);
        produceTaskDO.setProduceTaskId(id);
        return updateById(produceTaskDO);
    }

    @Override
    public ProduceVO getProduceByProductId(String batch,Long productId) {
        ProduceVO produceVO  = new ProduceVO();
        Wrapper<ProduceTaskDO> wrapper= new EntityWrapper<>();
        wrapper.eq("batch_no",batch);
        wrapper.eq("product_id",productId);
        wrapper.eq("state",0);
        ProduceTaskDO produceTaskDO= selectOne(wrapper);
        if (produceTaskDO != null){
            MarshallingDO marshallingDO = marshallingService.selectById(produceTaskDO.getMarshallingId());
            BeanUtils.copyProperties(produceTaskDO,produceVO);
            produceVO.setMarshallingName(marshallingDO.getMarshallingName());
        }
        return produceVO;
    }
}
